//
//  HZNetTools.m
//  网络工具类 OC
//
//  Created by 黑眼圈 on 16/4/18.
//  Copyright © 2016年 heiyan. All rights reserved.
//

#import "HZNetTools.h"


@protocol HZNetToolsDelegate <NSObject>

@optional
- (NSURLSessionDataTask *)dataTaskWithHTTPMethod:(NSString *)method
URLString:(NSString *)URLString
parameters:(id)parameters
uploadProgress:(nullable void (^)(NSProgress *uploadProgress)) uploadProgress
downloadProgress:(nullable void (^)(NSProgress *downloadProgress)) downloadProgress
success:(void (^)(NSURLSessionDataTask *, id))success
failure:(void (^)(NSURLSessionDataTask *, NSError *))failure;

@end


@interface HZNetTools()<HZNetToolsDelegate>



@end


@implementation HZNetTools




//单例
+(instancetype)sharedTools{
    
    static HZNetTools* tools;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        tools = [HZNetTools manager];
        //添加一种解析的格式
        tools.responseSerializer.acceptableContentTypes = [tools.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
         tools.responseSerializer.acceptableContentTypes = [tools.responseSerializer.acceptableContentTypes setByAddingObject:@"text/plain"];
    });
    
    return tools;
    
}


#pragma mark -
#pragma mark     网络解析
-(void)requestWithMethod:(HZRequestMethod)method urlString:(NSString*)urlstring parameters:(id)parameters finishedBlock:(void(^)(id responstObject,NSError *error))finishedBlock{
    
    NSString* methodString;
    
    //判断请求类型
    if (method == HZRequestMethodGet) {
        
        methodString = @"GET";
    }else{
        methodString = @"post";
    }
  
    //请求
    [[self dataTaskWithHTTPMethod:methodString URLString:urlstring parameters:parameters uploadProgress:nil downloadProgress:nil success:^(NSURLSessionDataTask * dataTask, id responstObject) {
        
        //成功的 block 回调
        finishedBlock(responstObject,nil);
        
        
    } failure:^(NSURLSessionDataTask * dataTask, NSError * error) {
        
        //失败的 block 回调
        finishedBlock( nil,error);
    }] resume];
    
}




@end
